package com.example.gaodemap.controller;

import com.example.gaodemap.dto.StaticMapRequest;
import com.example.gaodemap.service.GaodemapService;
import jakarta.validation.Valid;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.net.URLEncoder;
import java.nio.charset.StandardCharsets;
import java.util.HashMap;
import java.util.Map;

@RestController
@RequestMapping("/api/map")
@CrossOrigin(origins = "*")
public class GaodemapController {
    @Autowired
    private GaodemapService gaodemapService;

    @PostMapping("/url")
    public ResponseEntity<Map<String, String>> generateMapUrl(@Valid @RequestBody StaticMapRequest request) {
        System.out.println(request);
        try {
            String url = gaodemapService.generateStaticMapUrl(request);
            Map<String, String> response = new HashMap<>();
            response.put("url", url);
            response.put("status", "success");
            return ResponseEntity.ok(response);
        } catch (Exception e) {
            Map<String, String> response = new HashMap<>();
            response.put("error", e.getMessage());
            response.put("status", "error");
            return ResponseEntity.badRequest().body(response);
        }
    }

    @PostMapping("/image")
    public ResponseEntity<byte[]> getMapImage(@Valid @RequestBody StaticMapRequest request) {
        try {
            byte[] imageData = gaodemapService.getStaticMapImage(request);
            HttpHeaders headers = new HttpHeaders();
            headers.setContentType(MediaType.IMAGE_PNG);
            headers.setContentLength(imageData.length);
            return new ResponseEntity<>(imageData, headers, HttpStatus.OK);
        } catch (Exception e) {
            return ResponseEntity.badRequest().build();
        }
    }

    @PostMapping("/download")
    public ResponseEntity<byte[]> downloadMapImage(@Valid @RequestBody StaticMapRequest request) {
        try {
            byte[] imageData = gaodemapService.getStaticMapImage(request);
            HttpHeaders headers = new HttpHeaders();
            headers.setContentType(MediaType.APPLICATION_OCTET_STREAM);
            headers.setContentLength(imageData.length);
            String filename = URLEncoder.encode("gaode_map.png", StandardCharsets.UTF_8);
            headers.set(HttpHeaders.CONTENT_DISPOSITION, "attachment; filename=" + filename);
            return new ResponseEntity<>(imageData, headers, HttpStatus.OK);
        } catch (Exception e) {
            return ResponseEntity.badRequest().build();
        }
    }

    @GetMapping("/simple")
    public ResponseEntity<Map<String, String>> getSimpleMapUrl(
            @RequestParam String longitude,
            @RequestParam String latitude,
            @RequestParam(defaultValue = "10") Integer zoom) {
        try {
            String url = gaodemapService.generateSimpleMapUrl(longitude, latitude, zoom);
            Map<String, String> response = new HashMap<>();
            response.put("url", url);
            response.put("status", "success");
            return ResponseEntity.ok(response);
        } catch (Exception e) {
            Map<String, String> response = new HashMap<>();
            response.put("error", e.getMessage());
            response.put("status", "error");
            return ResponseEntity.badRequest().body(response);
        }
    }

    @GetMapping("/health")
    public ResponseEntity<Map<String, String>> health() {
        Map<String, String> response = new HashMap<>();
        response.put("status", "UP");
        response.put("service", "Gaodemap Static Map Service");
        return ResponseEntity.ok(response);
    }
} 